<?php

namespace My\Controller;

use Utuo\BackgroundController;

class SiteArticleController extends BackgroundController {
	// 状态
	private $status = array(
			0 => '草稿',
			1 => '发布' 
	);
	
	// 栏目id
	private $catId;
	
	// 初始化
	public function _initialize() {
		$this->catId = $catId = I('cat_id', 0, 'intval');
		$this->setControllerTitle('文章管理');
		$this->setActionTitle(array(
				'modify' => '修改文章',
		));
		$this->setActionTitle('list', '文章列表', "cat_id=$catId");
		$this->setActionTitle('add', '添加文章', "cat_id=$catId");
		$this->setActionTitle('recycle', '回收站', "cat_id=$catId");
		$this->setActionTitle('index', '全部文章', "cat_id=$catId");
		$this->setActionNav('index,add,recycle');
		$this->assign('cat_id', $catId);
	}
	
	public function indexAction() {
		$this->getLists( 0 );
	}
	
	public function recycleAction() {
		$this->getLists( 1 );
	}
	
	
	public function categoryModify($action) {
		$this->setActionNav(false);
		$ArticleCategory = M("ArticleCategory")->find($this->catId);
		$this->assign ( "articleCategory", $ArticleCategory );
		$this->display ($action);
	}
	
	
	// 获取列表
	private function getLists($is_deleted) {
		$d = D("Article");
		$page = $this->getPage();
		$pagesize = 10;
		$condition = array(
				'is_deleted' => $is_deleted,
				'cid' => $this->aid,
				'cat_id' => $this->catId 
		);
		$articleLists = $d->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		$articleCount = $d->where($condition)->count();
		foreach ($articleLists as &$v) {
			$v['tags'] = $d->getTagNames($v['tags'], $this->aid);
		}
		$this->assign('articleLists', $articleLists);
		$this->showPage($articleCount, $pagesize);
		$this->display();
	}
	
	// 添加文章页面
	public function addAction() {
		$this->assign('status', $this->getRadio('status', $this->status, 1));
		$this->display();
	}
	
	// 添加文章
	public function addPostAction() {
		$dArticle = D('Article');
		$dArticleConfig = D('ArticleConfig');
		if ($dArticle->create()) {
			
			$dArticle->cid = $this->aid;
			$dArticleConfig->content = htmlspecialchars_decode(I('content', ""));
			$dArticleConfig->cid = $this->aid;
			$id = $dArticle->add();
			if ($id) {
				$dArticleConfig->id = $id;
				if ($dArticleConfig->add()) {
					$this->ajaxSuccess('添加文章成功');
				} else {
					$this->ajaxError('保存文章内容失败');
				}
			} else {
				$this->ajaxError($dArticle->getError());
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
	
	// 修改页面
	public function modifyAction() {
		$d = D('Article');
		$id = I('id', 0, 'intval');
		$article = $d->getById($id);
		$article = array_merge($article, M('ArticleConfig')->find($id));
		$article['tags'] = $d->getTagNames($article['tags'], $this->aid);
		$article['status'] = $this->getRadio('status', $this->status, $article['status']);
		$this->checkAdmin($article['cid']);
		$article['content'] = htmlspecialchars_decode($article['content']);
		$this->assign('article', $article);
		$this->display();
	}
	
	// 提交修改
	public function modifyPostAction() {
		$dArticle = D('Article');
		$dArticleConfig = D('ArticleConfig');
		
		if ($dArticle->create() && $dArticleConfig->create()) {
			$dArticle->cid = $this->aid;
			$dArticleConfig->cid = $this->aid;
			if (false !== $dArticle->save() && false !== $dArticleConfig->save()) {
				$this->ajaxSuccess('修改文章成功');
			} else {
				$this->ajaxError($dArticle->getError() . $dArticleConfig->getError());
			}
		} else {
			$this->ajaxError($dArticle->getError() . $dArticleConfig->getError());
		}
	}
	
	// 移除菜单
	public function removePostAction() {
		$this->ajaxRemove('Article');
	}
	
	// 恢复菜单
	public function restorePostAction() {
		$this->ajaxRestore('Article');
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete('Article');
		$this->ajaxDelete('ArticleConfig');
	}
	
	// 设置状态
	public function setStatusPostAction() {
		$this->ajaxSetStatus('Article');
	}
	// 设置排序
	public function setSortPostAction() {
		$this->ajaxSort('Article');
	}
}